package com.stream;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {

        //collect(Collectors.toList())由 Stream 里的值生成一个列表,是一个及早求值操作
        List<Integer> intList = Stream.of(1, 2, 3, 4, 5)
                .collect(Collectors.toList());
        System.out.println("collect: "+intList);

        //map可以将一种类型的值转换成另外一种类型
        List<Integer> mapList = intList.stream()
                .map(s -> s * s)
                .collect(Collectors.toList());
        System.out.println("map: "+mapList);

        //filter保留 Stream中的一些元素,而过滤掉其他的
        List<Integer> filterList = intList.stream()
                .filter(s -> s > 3)
                .collect(Collectors.toList());
        System.out.println("filter: "+filterList);

        //flatMap然 后 将 多 个 Stream 连 接 成 一 个 Stream
        List<Integer> flatMapList = Stream.of(mapList, intList)
                .flatMap(s -> s.stream())
                .collect(Collectors.toList());
        System.out.println("flatMap: "+flatMapList);
        //max,min 求最大值和最小值
        Integer min = intList.stream()
                .min(Integer::compareTo)
                .get();
        Integer max = intList.stream()
                .max(Integer::compareTo)
                .get();
        System.out.println("min:"+min+" max:"+max);

        //reduce 操作可以实现从一组值中生成一个值
        Integer reduce = intList.stream()
                .reduce(0, (acc, element) -> acc + element);
        Optional<Integer> reducei = intList.stream()
                .reduce((acc, element) -> acc + element);
        System.out.println("reduce:"+reduce);

        //distinct去重
        Stream.of(1,2,3,1,2,3)
                .distinct()
                .forEach(System.out::println);

        //sorted 排序
        Stream.of(1,2,3,1,2,3)
                .sorted()
                .forEach(System.out::println);
        //skip 过滤掉原Stream中的前N个元素
        Stream.of(1, 2, 3,4,5)
                .skip(2)
                .forEach(System.out::println);
        //limit 截取前N个数据
        Stream.of(1, 2, 3, 4, 5)
                .limit(2)
                .forEach(System.out::println);

        //迭代器 需要一个种子数 生成数据 f(n), f(f(n)), f(f(f(n)))
        Stream.iterate(1, n -> n+3)
                .limit(10)
                .forEach(n-> System.out.print(n+" "));
        System.out.println();
        //生成器 f(n), f(n), f(n)
        Stream.generate(()->3)
                .limit(10)
                .forEach(n-> System.out.print(n+" "));
        System.out.println();

        //所有匹配
        boolean allMatch = Stream.of(1, 2, 3, 4)
                .allMatch(integer -> integer > 10);
        System.out.println("allMatch: " + allMatch);
        //任何一个匹配上
        boolean anyMatch = Stream.of(1, 2, 3, 4)
                .anyMatch(integer -> integer > 10);
        System.out.println("anyMatch: " + anyMatch);
        //所有不匹配
        boolean noneMatch = Stream.of(1, 2, 3, 4, 5)
                .noneMatch(integer -> integer > 10);
        System.out.println("noneMatch:" + noneMatch);

        //并行 parallel/parallelStream
        Integer parallelStream = intList.parallelStream()
                .reduce(0, (acc, emel) -> acc + emel);
        System.out.println("parallelStream:"+parallelStream);

        Integer parallel = Stream.of(1, 2, 3, 4, 5)
                .parallel()
                .reduce(0, (acc, emel) -> acc + emel);
        System.out.println("parallel:"+parallel);


    }
}
